package kuba.daos;

import java.util.Collection;

import kuba.bean.Author;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class AuthorSqlDAO implements AuthorDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public Collection<Author> getAll() {
        
        return jdbcTemplate.query("SELECT * FROM AUTHORS", new BeanPropertyRowMapper<Author>(Author.class) );
    }
    @Override
    public void delete(Author author) {
        String sql = "DELETE FROM AUTHORS WHERE ID = ?";
        jdbcTemplate.update(sql, new Object [] {author.getId()});
    }
    @Override
    public void update (Author author) {
        String sql = "UPDATE AUTHORS SET NAME=?, LAST_NAME=? WHERE ID =? ";
        jdbcTemplate.update(sql, new Object [] {author.getName(),author.getLastName(),author.getId()});
    }
    @Override
    public void insert (Author author) {
        String sql = "INSERT INTO AUTHORS VALUES (?,?,?)";
        jdbcTemplate.update(sql, new Object [] {0,author.getName(),author.getLastName()});
    }

}
